package cn.wit.summit.chwan.widgets;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dbmanager.DownloadTaskManager;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.LogUtils;

/**
 * Created by Alan on 2018/4/13.
 */

public class RecommendTopDownloadView extends LinearLayout {

    private TextView totalDownloadTx;
    private int totalDownloadNum;
    private ImageView downloadStopIv;
    private ImageView downloadingIv;
    private LinearLayout downloadLl;

    private AnimationDrawable anim;

    public RecommendTopDownloadView(Context context) {
        super(context);
        initView(context);
    }

    public RecommendTopDownloadView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.part_top_download_view, null);
        totalDownloadTx = rootView.findViewById(R.id.totalDownloadTx);
        downloadLl = rootView.findViewById(R.id.downloadLl);
        downloadStopIv = rootView.findViewById(R.id.downloadStopIv);
        downloadingIv = rootView.findViewById(R.id.downloadingIv);

        anim = (AnimationDrawable) getResources().getDrawable(R.drawable.top_download_drawable);
        downloadingIv.setImageDrawable(anim);
//        anim.start();

        this.removeAllViews();
        this.addView(rootView);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtil.getInstance().goDownloadCenterActivity(getContext());
            }
        });
    }

    public int getTotalDownloadNum() {
        return totalDownloadNum;
    }

    public void setTotalDownloadNum() {
        try {
            DownloadTaskManager downloadTaskManager = DownloadTaskManager.getInstance();
            List<DownLoadTaskTable> dataList = downloadTaskManager.findNotDownFinishNumber();
            if (dataList!=null && dataList.size()>0) {
                setTotalDownloadNum(dataList.size());
            } else {
                setTotalDownloadNum(0);
            }
        } catch (Exception e) {
            LogUtils.e("RecommendTopDownloadView>>>>e:"+e.getMessage());
        }
    }

    public void setTotalDownloadNum(int totalDownloadNum) {
        this.totalDownloadNum = totalDownloadNum;
        totalDownloadTx.setText(""+totalDownloadNum);
        if (totalDownloadNum<=0) {
            setStopAnim();
        } else if (totalDownloadNum>0) {
            setStartAnim();
        }
        invalidate();
    }

    public void setStartAnim() {
        downloadLl.setVisibility(VISIBLE);
        downloadStopIv.setVisibility(GONE);
        anim.start();
    }

    public void setStopAnim() {
        downloadLl.setVisibility(GONE);
        downloadStopIv.setVisibility(VISIBLE);
        anim.stop();
    }

    public boolean isRun() {
        if (downloadLl.getVisibility()==VISIBLE)
            return true;
        else
            return false;
    }
}
